import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../config/public_text.dart';
import '../../foundation/style/normal_text_style.dart';
import '../button/action_button.dart';
import '../container/radius_container.dart';
import '../container/separated_row.dart';
import '../text_field/public_text_field.dart';

/// FileName public_search_bar
///
/// @Author lax
/// @Date 2024/4/10
///
/// @Description

class PublicSearchBar extends StatelessWidget {
  final String? text;
  late final String? hintText;
  final String? buttonText;

  final bool enable;
  final bool? buttonEnable;
  final bool? showButton;

  final Widget? buttonWidget;

  final TextStyle? textStyle;
  final TextStyle? hintTextStyle;
  final TextStyle? buttonTextStyle;

  final EdgeInsets? padding;

  final Color? color;
  final Color? barColor;

  final double? height;
  final double? radius;

  final ValueChanged<String>? onChanged;

  final VoidCallback? onPressedButton;

  final FocusNode? focusNode;

  PublicSearchBar({
    super.key,
    this.text,
    String? hintText,
    this.buttonText,
    this.enable = true,
    this.buttonEnable = true,
    this.showButton = false,
    this.buttonWidget,
    this.textStyle,
    this.hintTextStyle,
    this.buttonTextStyle,
    this.padding = const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
    this.color,
    this.barColor,
    this.height,
    this.radius,
    this.onChanged,
    this.onPressedButton,
    this.focusNode,
  }) {
    this.hintText = hintText ?? PublicText.search;
  }

  @override
  Widget build(BuildContext context) {
    return SeparatedRow(
      color: barColor,
      padding: padding ?? EdgeInsets.zero,
      separator: const SizedBox(width: 8),
      children: [
        Expanded(
          child: RadiusContainer(
            height: height ?? 44,
            radius: radius ?? 22,
            color: color ?? PublicColor.background,
            child: PublicTextField(
              text: text,
              style: textStyle,
              enable: enable,
              unableShowHintText: true,
              focusNode: focusNode,
              padding: const EdgeInsets.all(12),
              hintText: hintText,
              hintStyle: hintTextStyle ??
                  NormalTextStyle(color: PublicColor.lightText),
              onChanged: (value) {
                onChanged?.call(value);
              },
            ),
          ),
        ),
        Visibility(
          visible: showButton == true,
          child: SizedBox(
            height: 44,
            child: searchButton(),
          ),
        ),
      ],
    );
  }

  Widget searchButton() {
    if (buttonWidget != null) {
      return buttonWidget!;
    }
    return ActionButton(
      onPressed: onPressedButton,
      child: Text(
        buttonText ?? PublicText.search,
        textAlign: TextAlign.center,
        style: buttonTextStyle ?? NormalTextStyle(color: PublicColor.darkText),
      ),
    );
  }
}
